package com.spic.business.utils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : 赵祥
 * create at:  2022/11/25  17:08
 * @Description :
 **/
public class GroupUtils {
    /**
     * 数据分组
     *
     * @param sourceList 原集合
     * @param groupNum 每组的个数
     * @return
     */
    public static <T> List<List<T>> groupData(List<T> sourceList, int groupNum) {
        List<List<T>> targetList = new ArrayList<>();
        int size = sourceList.size();
        int remainder = size % groupNum;
        int sum = size / groupNum;
        for (int i = 0; i < sum; i++) {
            List<T> subList;
            subList = sourceList.subList(i * groupNum, (i + 1) * groupNum);
            targetList.add(subList);
        }
        if (remainder > 0) {
            List<T> subList;
            subList = sourceList.subList(size - remainder, size);
            targetList.add(subList);
        }
        return targetList;
    }
}
